# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import fields
from osv import osv
import wizard
import netsvc
import pooler
import time
from osv.orm import browse_record

class wiz_project_budget_detail_open(wizard.interface):
    
    def _open_budget_detail(self, cr, uid, data, context):
        #raise osv.except_osv("E",data)
        pool_obj = pooler.get_pool(cr.dbname)
        try:
            prj_id = data['ids'][0]
        except:
            prj_id = data['id']
        
        cr.execute("Select id,name from ir_ui_view where name in ('kdvn.budget.data.view.form','kdvn.budget.data.view.for.input.tree')")
        #pool_obj = pooler.get_pool(cr.dbname)
        
        #model_data_ids = pool_obj.get('ir.ui.view').search(cr,uid,[('name','=','kdvn.budget.data.view.form')])
        #if model_data_ids:
        #    form_view_id = model_data_ids[0]
            
        #model_data_ids = pool_obj.get('ir.ui.view').search(cr,uid,[('name','=','kdvn.budget.data.view.for.input.tree')])
        #if model_data_ids:
        #   tree_view_id = model_data_ids[0]
        res = cr.fetchall()
        if len(res)==2:
            if res[1][1]=='kdvn.budget.data.view.for.input.tree':
                tree_view_id = res[1][0]
                form_view_id =  res[0][0]
            else:
                tree_view_id = res[0][0]
                form_view_id =  res[1][0]
        else:
             tree_view_id = 0
             form_view_id = 0
        prj_obj = pool_obj.get('kdvn.project').read(cr,uid,prj_id,['name','code'])
        prj_code = ''
        prj_name = ''
    #    raise osv.except_osv("E",prj_obj)
        
        prj_code = prj_obj['code']
        prj_name = prj_obj['name']
        
        proj_name = ''
        if prj_code:
            proj_name = ": "+ prj_code+" - " + prj_name
        
        value = {
            'domain': "[('project_id', '=',"+str(prj_id)+")]",
            'name': 'Input Budget Data'+proj_name,
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kdvn.budget.data',
            'limit':25,
            'context':"{'prj_id':"+str(prj_id)+"}",
            'views': [(tree_view_id,'tree'),(form_view_id,'form')],
            'type': 'ir.actions.act_window'
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_budget_detail, 'state':'end'}
        }
    }
wiz_project_budget_detail_open('project_budget_detail_open')

class wiz_job_budget_is_zero_open(wizard.interface):
    
    def _open_job_budget_zero(self, cr, uid, data, context):
        
        cr.execute("Select kp.id from kdvn_project kp left join kdvn_budget_data kbd on kp.id = project_id group by kp.id having sum(coalesce(budget_amount,0))=0")
        job_ids = []
        for id in cr.fetchall():
            job_ids.append(id)
        job_ids = ",".join(map(str,job_ids))
        
        value = {
            'domain': "[('id', 'in',["+str(job_ids)+"])]",
            'name': 'Job',
            'view_type': 'form',
            'view_mode': 'tree,form',
            'res_model': 'kdvn.project',
            'type': 'ir.actions.act_window'
        }
        return value

    states = {
        'init' : {
            'actions' : [],
            'result' : {'type':'action', 'action':_open_job_budget_zero, 'state':'end'}
        }
    }
wiz_job_budget_is_zero_open('wiz_job_budget_is_zero_open')